package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.entity.ExpPayReq;
import com.softer.wxzj.entity.ExpPayReqDetail;
import com.softer.wxzj.entity.ExpPayReqList;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.service.ExpPayReqDetailService;
import com.softer.wxzj.service.ExpPayReqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 拨款管理
 *
 * @author Gong Zhiyang
 * @date 2019/7/17
 */
@RestController
@RequestMapping("/expPayReq")
public class ExpPayReqController {
    @Autowired
    private ExpPayReqService expPayReqService;
    @Autowired
    private ExpPayReqDetailService expPayReqDetailService;

    /**
     * 项目拨款管理列表
     *
     * @param page          分页实体
     * @param expProjectReq 查询实体
     * @return 列表
     */
    @PostMapping("/getPayReqList")
    @LogRecord(description = "项目拨款管理列表", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.SELECT)
    public PageInfo<ExpProjectReq> getPayReqList(Page page, ExpProjectReq expProjectReq, String fuzzy) {
        return expPayReqService.getPayReqList(page, expProjectReq, fuzzy);
    }

    /**
     * 新增拨款管理
     *
     * @param expPayReqList list   拨款批次
     *                      isLast 是否存在质保金
     *                      num    拨款批次数
     *                      reqId  项目ID
     * @return 新增结果
     */
    @PostMapping("/add")
    @LogRecord(description = "新增拨款管理", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.INSERT)
    public CommonResult add(@RequestBody ExpPayReqList expPayReqList) {
        return expPayReqService.add(expPayReqList);
    }


    /**
     * 修改拨款管理
     *
     * @param expPayReqList list   拨款批次
     *                      isLast 是否存在质保金
     *                      num    拨款批次数
     *                      reqId  项目ID
     * @return 新增结果
     */
    @PostMapping("/update")
    @LogRecord(description = "修改拨款管理", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.UPDATE)
    public CommonResult update(@RequestBody ExpPayReqList expPayReqList) {
        return expPayReqService.update(expPayReqList);
    }

    /**
     * 拨款申请
     *
     * @param id 拨款ID
     * @return 申请状态
     */
    @PostMapping("/updateState")
    @LogRecord(description = "拨款申请", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.UPDATE)
    public CommonResult updateState(String id, String operNo) {
        return expPayReqService.updateState(id, operNo);
    }

    /**
     * 判断是否进行过拨款，用于前端跳页
     *
     * @param reqId
     * @return
     */
    @PostMapping("/getIsGrant")
    @LogRecord(description = "判断是否进行过拨款", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.SELECT)
    public int getIsGrant(String reqId) {
        return expPayReqService.getIsGrant(reqId);
    }

    /**
     * 拨款管理增加
     *
     * @param expPayReqList
     * @return
     */
    @PostMapping("/insertExpPayReqByList")
    @LogRecord(description = "拨款管理增加", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.INSERT)
    public CommonResult insertExpPayReqByList(@RequestBody ExpPayReqList expPayReqList) {
        return expPayReqService.add(expPayReqList);
    }

    /**
     * 拨款管理修改
     *
     * @param expPayReqList
     * @return
     */
    @PostMapping("/updateExpPayReqByList")
    @LogRecord(description = "拨款管理修改", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.UPDATE)
    public CommonResult updateExpPayReqByList(@RequestBody ExpPayReqList expPayReqList) {
        return expPayReqService.updateExpPayReqByList(expPayReqList);
    }

    /**
     * 根据redId查询拨款管理详细
     *
     * @param reqId   项目ID
     * @param arcType 图档类型
     * @return
     */
    @RequestMapping("/getListByReqId")
    @LogRecord(description = "查询拨款管理详细", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.SELECT)
    public List<ExpPayReq> getListByReqId(String reqId, String arcType) {
        return expPayReqService.getList(reqId, arcType);
    }

    /**
     * 拨款申请
     *
     * @param expPayReq
     * @return
     */
    @RequestMapping("/grantApply")
    @LogRecord(description = "拨款申请", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.UPDATE)
    public CommonResult grantApply(ExpPayReq expPayReq) {
        return expPayReqService.grantApply(expPayReq);
    }

    /**
     * 拨款申请批次详情打印
     *
     * @param id 拨款批次id
     * @return 拨款详情
     */
    @RequestMapping("/selectByPayReqIdForm")
    @LogRecord(description = "拨款申请批次详情打印", operationUnit = LogOperationUnit.EXP_PAY_REQ, operationType = LogOperationType.SELECT)
    public PageInfo<ExpPayReqDetail> selectByPayReqId(String id, Page page) {
        return expPayReqDetailService.selectByPayReqId(id, page);
    }
}
